<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import qrcodeBorder from 'assets/images/qrcode-border.png';
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    export default{
        data(){
            return {
                qrcodeBorder: qrcodeBorder, //二维码框
                qrcodeUrl: null, // 扫描二维码签约
                payRemark: null, // 扫码说明文字
                info: [], //免密支付说明
                signTyp: null, //签约类型
                payId: null, //支付id
                title: null,
                payMoney: 0,
                timeout: 3000 //3s调用一次监听接口
            }
        },
        created() {
            let bundleUrl = weex.config.bundleUrl;
            let options = bundleUrl.split('options=')[1];
            options = JSON.parse(options);

            if(typeof options.payId === 'undefined') {
                Modal.toast('payId支付ID参数未传!');
                return false;
            }

            if(typeof options.signType === 'undefined'){
                Modal.toast('signType签约类型未知!');
                return false;
            }

            if(typeof options.img === 'undefined'){
                Modal.toast('signImg签约二维码地址未传!');
                return false;
            }

            this.signType = options.signType;
            this.qrcodeUrl = options.img;
            this.info = options.info;
            this.payId = options.payId;
            this.payMoney = options.payMoney;
            this.title = options.title;
            this.payRemark = options.remark;

            this.start();//启动监听
        },
        methods: {
             /**
             * @description 定时器开始函数
             */
            start(){
                /**采用轮询机制调用查询接口*/
                if(!this.timer) this.timer = setInterval(this.getSignInfo.bind(this), this.timeout);
            },

            /**
             * @description 查询签约免密支付信息函数
             */
            getSignInfo(){
                let self = this;
                //轮循请求支付成功接口，判断是否支付
                api.sendHttp("getPayQueryApi",{payid: this.payId,timeout: this.timeout},false).then(res => {
                    self.stop();
                    navigator.pop();
                    let data = res.data || {};
                    let _params = {
                         payId: this.payId,
                         tradenum: data.tradenum || 0,
                         transactioninfo: data.transactioninfo || ''
                    };
                    let options = {
                        classType: constant.PaySuccess_Class_Type,
                        title: '收银台',
                        params: _params
                    };
                    navigator.push(options);
                }, res => {
                    console.log(res.msg);
                });
            },

            /**
             * @description 定时器结束函数
             */
            stop() {
                clearInterval(this.timer);
                this.timer = null;
            }
        },

        destroyed(){
            this.stop();//组件销毁
        }
    }
</script>
